/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.service.dao;

import com.guhesan.querycrypt.QueryCrypt;
import com.guhesan.querycrypt.beans.RequestParameterObject;
import com.myapp.service.bo.ActionResultBO;
import com.myapp.util.ResourceBundleUtil;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;

/**
 *
 * @author toint
 */
public class OracleDAOFactory extends DAOFactory {

    public static final String DAO_PAKAGE = ResourceBundleUtil.getDAOPackage();

    public Object executeMethod(ActionForm form, HttpServletRequest req)
            throws Exception {
        Object result = new Object();
        String className = "";
        String methodName = "";

        String checkCrypt = req.getParameter("lt4634ed9ce6d4cccdbc230a40c75d0172");

        RequestParameterObject reqCrypt = null;

        if (checkCrypt != null) {
            reqCrypt = QueryCrypt.decrypt(req);
        }

        Class[] classArg = {Class.forName("org.apache.struts.action.ActionForm"), Class.forName("javax.servlet.http.HttpServletRequest")};

        Object[] objectArg = {form, req};

        if (checkCrypt != null) {
            className = DAO_PAKAGE + reqCrypt.getParameter("className");
            methodName = reqCrypt.getParameter("methodName");
        } else {
            className = req.getParameter("className");

            if (className == null) {
                throw new Exception("Không tìm thấy giá trị className trong request");
            }

            className = DAO_PAKAGE + className;
            methodName = req.getParameter("methodName");
        }

        if (methodName == null) {
            throw new Exception("Không tìm thấy giá trị methodName trong request");
        }

        try {
            Class classObj = Class.forName(className);
            Method method = classObj.getMethod(methodName, classArg);

            Object obj = method.invoke(classObj.newInstance(), objectArg);
            result = obj;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }

        return result;
    }

    public void executeMethod(ActionForm form, HttpServletRequest req, Object result)
            throws Exception {
        String className = "";
        String methodName = "";

        String checkCrypt = req.getParameter("lt4634ed9ce6d4cccdbc230a40c75d0172");

        RequestParameterObject reqCrypt = null;

        if (checkCrypt != null) {
            reqCrypt = QueryCrypt.decrypt(req);
        }

        Class[] classArg = {Class.forName("org.apache.struts.action.ActionForm"), Class.forName("javax.servlet.http.HttpServletRequest"), Class.forName(ActionResultBO.class.getName())};

        Object[] objectArg = {form, req, result};

        if (checkCrypt != null) {
            className = DAO_PAKAGE + reqCrypt.getParameter("className");
            methodName = reqCrypt.getParameter("methodName");
        } else {
            className = req.getParameter("className");

            if (className == null) {
                throw new Exception("Không tìm thấy giá trị className trong request");
            }

            className = DAO_PAKAGE + className;
            methodName = req.getParameter("methodName");
        }

        if (methodName == null) {
            throw new Exception("Không tìm thấy giá trị methodName trong request");
        }

        try {
            Class classObj = Class.forName(className);
            Method method = classObj.getMethod(methodName, classArg);

            method.invoke(classObj.newInstance(), objectArg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
    }
}
